package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.domain.AjaxResult;
import com.itheima.common.domain.PageInfo;
import com.itheima.common.utils.StringUtils;
import com.itheima.order.entity.OmsOrderReturnReason;
import com.itheima.service.OmsOrderReturnReasonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
//后台退货原因
@Slf4j
@RestController
@RequestMapping("/returnReason")
public class OmsOrderReturnReasonController {

    @Autowired
    private OmsOrderReturnReasonService omsOrderReturnReasonService;

    /**
     * 添加退货原因
     *
     * @param omsOrderReturnReason
     * @return
     */
    @PostMapping("/create")
    public AjaxResult create(@RequestBody OmsOrderReturnReason omsOrderReturnReason) {
        omsOrderReturnReasonService.save(omsOrderReturnReason);
        return AjaxResult.success();
    }

    /**
     * 删除退货原因
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public AjaxResult delete(String ids) {
        List<Long> longs = StringUtils.toList(ids);
        omsOrderReturnReasonService.removeByIds(longs);
        return AjaxResult.success();
    }

    /**
     * 分页查询退货原因
     *
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(@RequestParam("pageSize") Integer pageSize, @RequestParam("pageNum") Integer pageNum) {
        Page<OmsOrderReturnReason> page = new Page<>(pageNum,pageSize);
        omsOrderReturnReasonService.page(page);
        PageInfo pageInfo = PageInfo.toPageList(page);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 修改状态
     *
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/update/status")
    public AjaxResult updateStatus(String ids, Integer status) {
        List<Long> longs = StringUtils.toList(ids);
        LambdaUpdateWrapper<OmsOrderReturnReason> luw = new LambdaUpdateWrapper<>();
        luw.in(OmsOrderReturnReason::getId, longs)
                .set(OmsOrderReturnReason::getStatus, status);
//                .set(OmsOrderReturnReason::getCreateTime, LocalDateTime.now())
        omsOrderReturnReasonService.update(luw);
        return AjaxResult.success();
    }

    /**
     * 修改退货原因
     *
     * @param omsOrderReturnReason
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public AjaxResult updateById(@RequestBody OmsOrderReturnReason omsOrderReturnReason, @PathVariable Long id) {
        LambdaUpdateWrapper<OmsOrderReturnReason> luw = new LambdaUpdateWrapper<>();
        luw.eq(OmsOrderReturnReason::getId, id)
                .set(omsOrderReturnReason.getCreateTime() != null, OmsOrderReturnReason::getCreateTime, omsOrderReturnReason.getCreateTime())
                .set(omsOrderReturnReason.getName() != null, OmsOrderReturnReason::getName, omsOrderReturnReason.getName())
                .set(omsOrderReturnReason.getSort() != null, OmsOrderReturnReason::getSort, omsOrderReturnReason.getSort())
                .set(omsOrderReturnReason.getStatus() != null, OmsOrderReturnReason::getStatus, omsOrderReturnReason.getStatus());
        omsOrderReturnReasonService.update(luw);
        return AjaxResult.success();
    }

    /**
     * 获取退货原因
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public AjaxResult getById(@PathVariable Long id) {
        OmsOrderReturnReason omsOrderReturnReason = omsOrderReturnReasonService.getById(id);
        return AjaxResult.success(omsOrderReturnReason);
    }

}
